﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TinyIM2013.Packets
{
    public class PReceive_0825_Redirect : PReceive
    {
        public byte[] ServerForRedirect { get; private set; }
        public PHeader PacketHead { get; private set; }
        public byte[] TokenData { get; private set; }
        internal bool TryParse(byte[] packetData, out PReceive_0825_Redirect packet)
        {
            packet = new PReceive_0825_Redirect();
            packet.PacketHead = PacketUtility.GetPHead(packetData);
            byte[] teaKey = IMKeyCollection.Instance.GetKey(packet.PacketHead.IMAccount, PCmd.CMD_0825);
            byte[] encryptedData = GetEnctyptedData(packetData);
            byte[] decryptedData = TEAUtility.Decrypt(encryptedData, 0, encryptedData.Length, teaKey);
            if (decryptedData != null)
            {
                packet.TokenData = GetTokenData(decryptedData);
                packet.ServerForRedirect = GetRedirectServer(decryptedData);
                return true;
            }
            else
            {
                return false;
            }
        }

        private byte[] GetRedirectServer(byte[] decryptedData)
        {
            byte[] returnBytes = new byte[4];
            try
            {
                Array.Reverse(decryptedData);
                Array.Copy(decryptedData, 6, returnBytes, 0, returnBytes.Length);
                Array.Reverse(returnBytes);
                return returnBytes;
            }
            catch
            {
                return null;
            }
        }

        private static byte[] GetTokenData(byte[] decryptedData)
        {
            byte[] returnBytes = new byte[56];
            try
            {
                Array.Copy(decryptedData, 5, returnBytes, 0, 56);
                return returnBytes;
            }
            catch
            {
                return null;
            }
            
        }

        internal byte[] GetEnctyptedData(byte[] packetData)
        {
            byte[] returnBytes = new byte[packetData.Length - 15];
            Array.Copy(packetData, 14, returnBytes, 0, packetData.Length - 15);
            return returnBytes;
        }

        public bool TryParse(byte[] packetData, out PReceive packet)
        {
            throw new NotImplementedException();
        }


        public byte[] GeneratePacket()
        {
            throw new NotImplementedException();
        }

        public void SendPacket()
        {
            throw new NotImplementedException();
        }

        bool PReceive.TryParse(byte[] packetData, out PReceive packet)
        {
            throw new NotImplementedException();
        }

        PHeader P.PacketHead
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        byte[] P.GeneratePacket()
        {
            throw new NotImplementedException();
        }

        void P.SendPacket()
        {
            throw new NotImplementedException();
        }
    }
}
